// Package repository
// Created by GoLand
// @User: lenora
// @Date: 2024/8/7
// @Time: 10:34

package repository

import (
	"camp/microbook/internal/domain"
	"camp/microbook/internal/repository/cache"
	"context"
)

type RankingRepository interface {
	Sync(ctx context.Context, arts []domain.Article) error
	GetTop(ctx context.Context) ([]domain.Article, error)
}

func NewRankingRepository(cache cache.RankingCache) RankingRepository {
	return &rankingRepository{
		cache: cache,
		//localCache: localCache,
	}
}

func NewRankingRepositoryV1(cache cache.RankingCache, localCache cache.RankingCache) RankingRepository {
	return &rankingRepository{
		cache:      cache,
		localCache: localCache,
	}
}

type rankingRepository struct {
	cache      cache.RankingCache
	localCache cache.RankingCache
}

func (repo *rankingRepository) GetTop(ctx context.Context) ([]domain.Article, error) {
	//res, err := repo.localCache.Get(ctx)
	//if err == nil {
	//	return res, nil
	//}
	res, err := repo.cache.Get(ctx)
	if err == nil {
		//_ = repo.localCache.Set(ctx, res)
	} else {
		//redis崩溃的情况下强制从本地缓存中获取,忽略ddl
	}

	return res, err
}

func (repo *rankingRepository) Sync(ctx context.Context, arts []domain.Article) error {
	//_ = repo.localCache.Set(ctx, arts)
	return repo.cache.Set(ctx, arts)
}
